/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        if (root == nullptr) return vector<int>();
        vector<int> v;
        vector<TreeNode*> vt;
        vt.push_back(root);

        while (vt.size())
        {
            int  n = vt[0]->val;
            vector<TreeNode*> tmp;
            for (auto& e : vt)
            {
                n = max(n, e->val);
                if (e->left) tmp.push_back(e->left);
                if (e->right) tmp.push_back(e->right);
            }
            v.push_back(n);
            vt = tmp;
        }
        return v;


    }
};